import java.sql.*;
import java.util.Scanner;

public class used_cars {
    //添加车辆
    public static void add() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.print("型号为:");String name=sc.next();
        System.out.print("厂商为:");String fen_lei=sc.next();
        System.out.print("价钱为:");int jia_qian=sc.nextInt();
        System.out.print("车龄为:");int car_age=sc.nextInt();
        System.out.print("行程数为:");int car_juli=sc.nextInt();

        Connection conn=JDBC.getConnection();

        //要执行的语句
        String sql="INSERT INTO used_cars(name,fen_lei,jia_qian,car_age,car_juli) VALUES('"+name+"','"+fen_lei+"',"+jia_qian+","+car_age+","+car_juli+")";
        PreparedStatement ps=null;
        //预处理
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        int set=0;
        if (ps != null) {
            set=ps.executeUpdate();
            System.out.println("添加成功");
        }

    }








    //展示车辆信息
    public static void zhanshi() throws SQLException {

        Connection conn=JDBC.getConnection();

        String sql="SELECT * FROM used_cars";
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            //执行查询
            ResultSet set=ps.executeQuery();
            ResultSetMetaData rsmd=set.getMetaData();
            System.out.println("(编号，型号，厂商，价钱，车龄，行程数)");
            //打表
            while(set.next()){
                for(int i=1;i<=rsmd.getColumnCount();i++){
                    System.out.print(set.getObject(i)+"\t");
                }
                System.out.println();
            }
        }


    }









    public static void updata() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要修改车辆编号为:");int id=sc.nextInt();


        Connection conn=JDBC.getConnection();


        String sql="DELETE FROM used_cars WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ps.executeUpdate();
            System.out.println("将数据修改为:");
            add();
        }else{
            System.out.println("不存在该车辆");
        }

    }











    public static void delete() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要删除的车辆编号为:");int id=sc.nextInt();
        Connection conn=JDBC.getConnection();

        String sql="DELETE FROM used_cars WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ps.executeUpdate();
            System.out.println("删除成功");
        }else{
            System.out.println("不存在");
        }

    }









    public static void query() throws SQLException {
        Scanner sc=new Scanner(System.in);
        System.out.println("要查询车辆编号为:");int id=sc.nextInt();

        Connection conn=JDBC.getConnection();

        String sql="SELECT * FROM used_cars WHERE id="+id;
        PreparedStatement ps=null;
        if (conn != null) {
            ps=conn.prepareStatement(sql);
        }
        if (ps != null) {
            ResultSet set=ps.executeQuery();
            ResultSetMetaData rsmd=set.getMetaData();
            System.out.println("(编号，型号，厂商，价钱，车龄，行程数)");
            while(set.next()){
                for(int i=1;i<=rsmd.getColumnCount();i++){
                    System.out.print(set.getObject(i)+"\t");
                }
                System.out.println();
            }
        }else{
            System.out.println("不存在");
        }

    }








    public static void carzhanshi() throws SQLException {
        System.out.println("现在有以下车辆");
        while (true){
            Scanner sc=new Scanner(System.in);
            zhanshi();
            System.out.println("你要执行的操作是:");
            System.out.println("1.添加车辆 2.修改车辆信息 3.删除车辆信息 4.查询车辆 5.切换用户需求页 6.退出系统");
            int choose=sc.nextInt();
            if(choose==1){
                add();
            }else if(choose==2){
                updata();
            }else if(choose==3){
                delete();
            }else if(choose==4){
                query();
            }else if(choose==5){

                remand.carneed();
            }else if(choose==6){
                break;
            }else{
                System.out.println("无效操作");
            }
        }
    }


}
